﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class ProductStorageImportExcel : SkinnedWebControl
    {
        private FileUpload upload;
        private IButtonControl btnConfirm;

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            upload = (FileUpload)skin.FindControl("upload");
            btnConfirm=(IButtonControl)skin.FindControl("btnConfirm");

            upload.FileExtensions = "xls|xlsm";

            btnConfirm.Click += new EventHandler(btnConfirm_Click);
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            if (upload.HasFile)
            {
                string fileUrl = upload.SaveAs();
                if (!string.IsNullOrEmpty(fileUrl))
                {
                    string filePath = Page.Server.MapPath(Globals.ApplicationPath.TrimEnd('/') + fileUrl);
                    string message = "数据导入成功。";
                    try
                    {
                        ProductStorage.ImportExcel(filePath);
                    }
                    catch
                    {
                        message = "数据导入失败，请检测excel文件中记录的格式是否正确。";
                    }

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertErrorMesage", string.Format("alert('{0}');", message), true);

                    Globals.FileDelete(filePath);
                }
            }
            else
            {
                SiteContext.RenderMessage(ExceptionType.FileNotFound);
            }
        }

        protected override string DefaultSkinFilename
        {
            get { return "ProductStorage/ProductStorageImportExcel.ascx"; }
        }
    }
}
